package com.wx.controller;

import com.wx.dto.BorrowDTO;
import com.wx.dto.BorrowPageQueryDTO;
import com.wx.entity.Borrow;
import com.wx.result.PageResult;
import com.wx.result.Result;
import com.wx.service.BorrowService;
import com.wx.vo.BorrowTimeVO;
import com.wx.vo.BorrowVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/borrow")
@Slf4j
/* 图书借阅控制类 */
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    /*借阅功能*/
    @GetMapping("/book")
    public Result borrowbook(BorrowDTO borrowDTO) {
        log.info("获取书籍信息：{}",borrowDTO);
        borrowService.borrowbook(borrowDTO);
        return Result.success();
    }

    /*根据用户borrow_id查询借书记录*/
    @GetMapping("/list")
    public Result borrowlist(Integer id){
        log.info("根据id查询借书记录：" + id);
        List<BorrowVO> list=borrowService.borrowList(id);
        return Result.success(list);
    }

    @GetMapping("/listr")
    public Result borrowlistR(Integer id){
        log.info("根据id查询借书记录：" + id);
        List<BorrowVO> list=borrowService.borrowListR(id);
        return Result.success(list);
    }

    /*根据借阅id查询借书信息*/
    @GetMapping("/borrowinfo")
    public Result borrowinfo(Integer id){
        log.info("根据借阅id查询信息");
        BorrowTimeVO borrowTimeVO = borrowService.borrowinfo(id);
        return Result.success(borrowTimeVO);
    }



    /*还书功能
    * 前端传入id
    * */
    @PutMapping("/return")
    public Result returnBook(@RequestBody Borrow borrow){
        log.info("根据id还书"+borrow);
        borrowService.returnBook(borrow.getId());
        return Result.success();
    }

    /*查询所有借阅记录*/
    @PostMapping("/borrowlist")
    public Result<PageResult> getBorrowList(@RequestBody BorrowPageQueryDTO borrowPageQueryDTO){
        log.info("借阅信息分页查询：{}",borrowPageQueryDTO);
        PageResult pageResult = borrowService.getBorrowList(borrowPageQueryDTO);
        return Result.success(pageResult);
    }
}
